package com.quanyan.place.service.impl;

import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.Page;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.constants.Constants;
import com.quanyan.place.constants.PlaceConstants;
import com.quanyan.place.entity.*;
import com.quanyan.place.entity.apireq.ReqEmployeeInfo;
import com.quanyan.place.entity.apireq.ReqSearchTargetCheckVo;
import com.quanyan.place.entity.apiresp.RespTargetCheck;
import com.quanyan.place.entity.vo.EmployeeFormDataVo;
import com.quanyan.place.mapper.CagetoryMapper;
import com.quanyan.place.mapper.TbBizPlaceEmployeeMapper;
import com.quanyan.place.mapper.TbBizPlaceTargetMapper;
import com.quanyan.place.service.EmployeeService;
import com.quanyan.place.service.TargetCheckService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by liuyaoxin on 2016/6/16
 */
@Service("targetCheckService")
public class TargetCheckServiceImpl implements TargetCheckService {

    private static final Logger logger = LoggerFactory.getLogger(TargetCheckServiceImpl.class);

    @Autowired
    TbBizPlaceEmployeeMapper tbBizPlaceEmployeeMapper;

    @Autowired
    TbBizPlaceTargetMapper tbBizPlaceTargetMapper;
    @Autowired
    CagetoryMapper cagetoryMapper;

    @Autowired
    EmployeeService employeeService;

    public   PageObj<List<TbBizPlaceEmployee>> getEmployeeList(ReqEmployeeInfo reqEmployeeInfo) {
        TbBizPlaceEmployeeExample example =new TbBizPlaceEmployeeExample();
        TbBizPlaceEmployeeExample.Criteria criteria=example.createCriteria();
        criteria.andParentIdEqualTo(reqEmployeeInfo.getId());//查询出下一级的成员
        int total=tbBizPlaceEmployeeMapper.countByExample(example);
        Integer pageNum=0;
        Integer pageSize=Constants.DEFAULT_PAGE_SIZE;
        pageNum= reqEmployeeInfo.getPageNum();
        pageSize= reqEmployeeInfo.getPageSize();
        example.setPage(new Page(pageNum * pageSize,pageSize,total));
        List<TbBizPlaceEmployee> list=tbBizPlaceEmployeeMapper.selectByExample(example);
        PageObj<List<TbBizPlaceEmployee>> result=PageObj.create(total,pageNum,pageSize,list);
        return result;
    }


    public TbBizPlaceEmployee  getKeyByUid(ReqSearchTargetCheckVo reqSearchTargetCheckVo){
        TbBizPlaceEmployeeExample example = new TbBizPlaceEmployeeExample();
        TbBizPlaceEmployeeExample.Criteria tbBizPlaceEmployeeCriteria = example.createCriteria();
        if (reqSearchTargetCheckVo!=null) {
            tbBizPlaceEmployeeCriteria.andUidEqualTo(reqSearchTargetCheckVo.getUid());
        }
        List<TbBizPlaceEmployee> list = tbBizPlaceEmployeeMapper.selectByExample(example);
        if (list!=null&&list.size()>0){
            TbBizPlaceEmployee employee=list.get(0);
             return  employee;
        }else{
          return null;
        }
    }
    /*
    根据自己的Id查看有没下级 有则返回1，无则返回0
     */
    public List<TbBizPlaceEmployee> getLowerLevelByParentId(Integer Id){
        //查看有没下级
        TbBizPlaceEmployeeExample employeeExample = new TbBizPlaceEmployeeExample();
        TbBizPlaceEmployeeExample.Criteria employeeCriteria = employeeExample.createCriteria();
        employeeCriteria.andParentIdEqualTo(Id);
         List<TbBizPlaceEmployee> parentIdlist= tbBizPlaceEmployeeMapper.selectByExample(employeeExample);
        if (null != parentIdlist && parentIdlist.size()>0){
            return parentIdlist;
        }else{
            return null;
        }
    }
    /*
    通过EmployeeId获取指标数据
     */
    public PageObj<List<RespTargetCheck>> getDataByeEmployeeId(ReqSearchTargetCheckVo reqSearchTargetCheckVo){
        Integer pageNum = 0;
        Integer pageSize = 15;
        TbBizPlaceTargetExample example = new TbBizPlaceTargetExample();
        TbBizPlaceTargetExample.Criteria tCriteria = example.createCriteria();
        tCriteria.andEmployeeIdIn(reqSearchTargetCheckVo.getIdList());
        if (!StringUtils.isEmpty(reqSearchTargetCheckVo.getCheckYear())&&!StringUtils.isEmpty(reqSearchTargetCheckVo.getCheckMonth())) {
            reqSearchTargetCheckVo.setCheckTime(reqSearchTargetCheckVo.getCheckYear() + reqSearchTargetCheckVo.getCheckMonth());
        }
        if (!StringUtils.isEmpty(reqSearchTargetCheckVo.getCheckTime())){
            tCriteria.andCheckTimeLike("%"+reqSearchTargetCheckVo.getCheckTime()+"%");
        }
        if (reqSearchTargetCheckVo.getType()!=null){
            tCriteria.andTypeEqualTo(reqSearchTargetCheckVo.getType());
        }
        int total = tbBizPlaceTargetMapper.countByExample(example);
        example.setPage(new Page(pageNum * pageSize, pageSize, total));
        List<TbBizPlaceTarget> list = tbBizPlaceTargetMapper.selectByExample(example);
        List<RespTargetCheck> listResp=new ArrayList<RespTargetCheck>();
        for (TbBizPlaceTarget target:list){
            RespTargetCheck check =new RespTargetCheck();
            check.setId(target.getId());
            check.setCheckTime(target.getCheckTime());
            TbBizPlaceEmployee employee=employeeService.getEmployeeById(target.getEmployeeId());
        if (employee!=null) {
            check.setName(employee.getName());
        }
            check.setType(target.getType());
            check.setTarget(target.getTarget());
            listResp.add(check);
        }
        PageObj<List<RespTargetCheck>> result = PageObj.create(total, pageNum, pageSize, listResp);
        return result;
    }



    @Override
    public APIResponse<PageObj<List<RespTargetCheck>>> getTargetCheckList(ReqSearchTargetCheckVo reqSearchTargetCheckVo) {
        TbBizPlaceEmployee evo=this.getKeyByUid(reqSearchTargetCheckVo);
        PageObj<List<TbBizPlaceTarget>> listTarget=null;
    if(evo!=null) {
    if (evo.getTitle().equals(PlaceConstants.TITLE_BUSSINESS_GENERAL_MANEGER)) {//如果职称是商务总经理
        CagetoryExample cagetoryExample = new CagetoryExample();
        CagetoryExample.Criteria eCriteria = cagetoryExample.createCriteria();
        List<Integer> valueList = new ArrayList<Integer>();
        valueList.add(1);
        valueList.add(2);
        valueList.add(3);
        valueList.add(4);
        eCriteria.andIdIn(valueList);
        List<Cagetory> listCagetory = cagetoryMapper.selectByExample(cagetoryExample);
        List<RespTargetCheck> formList = new ArrayList<RespTargetCheck>();
        for (Cagetory cagetory : listCagetory) {
            RespTargetCheck employeeVo = new RespTargetCheck();
            employeeVo.setName(evo.getName());
            employeeVo.setTitle(evo.getTitle());
            employeeVo.setId(evo.getId());
            //项目类型(1,蓝球，2，足球，3，羽毛球，4，网球)
            employeeVo.setType(cagetory.getId());
            if (this.getLowerLevelByParentId(evo.getId())!=null) {
                if (this.getLowerLevelByParentId(evo.getId()).size() > 0 && this.getLowerLevelByParentId(evo.getId()) != null) {
                    employeeVo.setBranch(1);
                } else {
                    employeeVo.setBranch(null);
                }
            }
                formList.add(employeeVo);
        }
        PageObj<List<RespTargetCheck>> result=new PageObj<List<RespTargetCheck>>();
        result.setItems(formList);
        /*
     //存储下级员工的指标信息
        result= this.getLowLevelTargetByid(evo.getId());
        if (result!=null) {
            if (result.getItems() != null && result.getItems().size() > 0) {
                List<RespTargetCheck> list = result.getItems();
                formList.addAll(list);
                result.setItems(formList);
            } else {
                result.setItems(formList);
            }
        }
        */
        return APIResponse.returnSuccess(result);
    } else {
        Integer pageNum = 0;
        Integer pageSize = Constants.DEFAULT_PAGE_SIZE;

        TbBizPlaceTargetExample example = new TbBizPlaceTargetExample();
        TbBizPlaceTargetExample.Criteria tCriteria = example.createCriteria();
        tCriteria.andEmployeeIdEqualTo(evo.getId());
        if (reqSearchTargetCheckVo != null) {
            pageNum = reqSearchTargetCheckVo.getPageNum();
            pageSize = reqSearchTargetCheckVo.getPageSize();
            if (!StringUtils.isEmpty(reqSearchTargetCheckVo.getCheckTime())){
                tCriteria.andCheckTimeLike("%"+reqSearchTargetCheckVo.getCheckTime()+"%");
            }
            if (reqSearchTargetCheckVo.getType()!=null){
                tCriteria.andTypeEqualTo(reqSearchTargetCheckVo.getType());
            }
        }
        int total = tbBizPlaceTargetMapper.countByExample(example);
        example.setPage(new Page(pageNum * pageSize, pageSize, total));
        List<TbBizPlaceTarget> list = tbBizPlaceTargetMapper.selectByExample(example);
        List<RespTargetCheck> listResp=new ArrayList<RespTargetCheck>();
        for (TbBizPlaceTarget target:list){
                RespTargetCheck check =new RespTargetCheck();
                check.setId(evo.getId());
                check.setCheckTime(target.getCheckTime());
                check.setName(evo.getName());
                check.setTitle(evo.getTitle());
                check.setType(target.getType());
                check.setTarget(target.getTarget());
            if (this.getLowerLevelByParentId(evo.getId())!=null) {
                if (this.getLowerLevelByParentId(evo.getId()).size() > 0 && this.getLowerLevelByParentId(evo.getId()) != null) {
                    check.setBranch(1);
                } else {
                    check.setBranch(0);
                }
            }
            listResp.add(check);
        }
        PageObj<List<RespTargetCheck>> result = PageObj.create(total, pageNum, pageSize, listResp);
         return APIResponse.returnSuccess(result);
    }
    }else{
        return APIResponse.returnFail("查询失败！");
    }
    }

    @Override
    public APIResponse importTargetCheckList(ReqSearchTargetCheckVo vo) {
         List<ReqSearchTargetCheckVo> list= vo.getListVo();
        for (ReqSearchTargetCheckVo reqSearchTargetCheckVo:list) {
            TbBizPlaceTarget voTarget = new TbBizPlaceTarget();
            voTarget.setEmployeeId(reqSearchTargetCheckVo.getEmployeeId());
            voTarget.setTarget(reqSearchTargetCheckVo.getTarget());
            voTarget.setType(reqSearchTargetCheckVo.getType());
            /*
            if (reqSearchTargetCheckVo.getType().equals("篮球")) {
                voTarget.setType(1);
            } else if (reqSearchTargetCheckVo.getType().equals("足球")) {
                voTarget.setType(2);
            } else if (reqSearchTargetCheckVo.getType().equals("羽毛球")) {
                voTarget.setType(3);
            } else {
                voTarget.setType(4);
            }
            */
            voTarget.setCheckTime(reqSearchTargetCheckVo.getCheckTime());

            TbBizPlaceTargetExample example = new TbBizPlaceTargetExample();
            TbBizPlaceTargetExample.Criteria criteria = example.createCriteria();
            criteria.andEmployeeIdEqualTo(reqSearchTargetCheckVo.getEmployeeId());
            criteria.andTypeEqualTo(reqSearchTargetCheckVo.getType());
            List<TbBizPlaceTarget> targetlist = tbBizPlaceTargetMapper.selectByExample(example);

            if (null != targetlist && targetlist.size()>0){
                TbBizPlaceTargetExample updateExample = new TbBizPlaceTargetExample();
                TbBizPlaceTargetExample.Criteria updateCriteria = updateExample.createCriteria();
                updateCriteria.andEmployeeIdEqualTo(reqSearchTargetCheckVo.getEmployeeId());
                updateCriteria.andTypeEqualTo(reqSearchTargetCheckVo.getType());
                tbBizPlaceTargetMapper.updateByExampleSelective(voTarget, updateExample);
            }else{
                tbBizPlaceTargetMapper.insert(voTarget);
            }

        }
        return APIResponse.returnSuccess();
    }

    @Override
    public APIResponse getLowLevelList(Integer id,Integer type) {
        TbBizPlaceEmployeeExample example = new TbBizPlaceEmployeeExample();
        TbBizPlaceEmployeeExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(id);//查询出下一级的成员
        List<TbBizPlaceEmployee> list = tbBizPlaceEmployeeMapper.selectByExample(example);
        List<Integer> ids=new ArrayList<Integer>();
        for(TbBizPlaceEmployee ee: list){
            ids.add(ee.getId());
        }
        List<RespTargetCheck> listVo = new ArrayList<RespTargetCheck>();
        if (ids!=null&&ids.size()>0) {
            //查询出下级的taret type等
            TbBizPlaceTargetExample tarExample =new TbBizPlaceTargetExample();
            TbBizPlaceTargetExample.Criteria empCriteria=tarExample.createCriteria();
            empCriteria.andEmployeeIdIn(ids);
            empCriteria.andTypeEqualTo(type);
            List<TbBizPlaceTarget> targetsList=tbBizPlaceTargetMapper.selectByExample(tarExample);
            if (null != targetsList && targetsList.size()>0){
                for (TbBizPlaceTarget vo:targetsList){
                    RespTargetCheck voTar=new RespTargetCheck();
                    voTar.setEmployeeId(vo.getEmployeeId());
                    TbBizPlaceEmployee employeeVo=  employeeService.getEmployeeById(vo.getEmployeeId());
                    if (employeeVo!=null) {
                        voTar.setName(employeeVo.getName());
                    }
                    voTar.setType(vo.getType());
                    voTar.setTarget(vo.getTarget());
                    voTar.setCheckTime(vo.getCheckTime());
                    listVo.add(voTar);
                }
            }else{
                for(TbBizPlaceEmployee ee: list) {
                    ids.add(ee.getId());
                    RespTargetCheck voTar=new RespTargetCheck();
                    voTar.setEmployeeId(ee.getId());
                    TbBizPlaceEmployee employeeVo=  employeeService.getEmployeeById(ee.getId());
                    if (employeeVo!=null) {
                        voTar.setName(employeeVo.getName());
                    }
                    voTar.setType(type);
                    listVo.add(voTar);
                }
            }
        }
        return APIResponse.returnSuccess(listVo);
    }
    @Override
    public PageObj<List<RespTargetCheck>>  getLowLevelTargetByid(Integer id) {

        //存储下级员工的指标信息
        if (this.getLowerLevelByParentId(id) != null) {
            List<TbBizPlaceEmployee> listVo = this.getLowerLevelByParentId(id);
            List<Integer> listInt = new ArrayList<Integer>();
            for (TbBizPlaceEmployee empVo : listVo) {
                listInt.add(empVo.getId());
            }
            ReqSearchTargetCheckVo reqSearchTargetCheckVo = new ReqSearchTargetCheckVo();
            reqSearchTargetCheckVo.setIdList(listInt);
            return this.getDataByeEmployeeId(reqSearchTargetCheckVo);
        }else {
            return null;
        }
    }

    @Override
    public PageObj<List<RespTargetCheck>> getLowLevelTargetByUid(Integer uid) {
        TbBizPlaceEmployee e= employeeService.getEmployeeByUid(uid);
        //存储下级员工的指标信息
        if (this.getLowerLevelByParentId(e.getId()) != null) {
            List<TbBizPlaceEmployee> listVo = this.getLowerLevelByParentId(e.getId());
            List<Integer> listInt = new ArrayList<Integer>();
            for (TbBizPlaceEmployee empVo : listVo) {
                listInt.add(empVo.getId());
            }
            ReqSearchTargetCheckVo reqSearchTargetCheckVo = new ReqSearchTargetCheckVo();
            reqSearchTargetCheckVo.setIdList(listInt);
            return this.getDataByeEmployeeId(reqSearchTargetCheckVo);
        }else {
            return null;
        }
    }


}
